#include "toolbar.h"

#include <stdlib.h>

#include "global.h"
#include "drawing.h"
#include "palette.h"

static const Button const buttons[] = {
	{ OPTION, TOOLBAR_SELECT, "selection" },
	{ SEPARATOR },
	{ DROPDOWN, 0, "roads", { 
		{ 0, "Street", "Street description" },
	} }
};


Toolbar* new_toolbar()
{
	int i;

	// create object
	Toolbar* toolbar = malloc(sizeof(Toolbar));
	toolbar->buttons = (Button*)buttons;
	toolbar->selected = 0;

	// create image
	int w = 0;
	for(i=0; i<(sizeof(buttons) / sizeof(Button)); i++)
		if(toolbar->buttons[i].type == SEPARATOR)
			w += SEPARATOR_W;
		else
			w += BUTTON_W;
	SDL_Surface* sf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, BUTTON_W, 8,
			0, 0, 0, 0);
	setup_palette(sf);
	SDL_FillRect(sf, NULL, TRANSPARENT);
	int x = 0;
	for(i=0; i<(sizeof(buttons) / sizeof(Button)); i++)
		if(toolbar->buttons[i].type != SEPARATOR)
		{
			fill_3d_box(sf, &(SDL_Rect) { x, 0, BUTTON_W, BUTTON_W },
					(i == toolbar->selected) ? LOWER 
					                         : UPPER);
			if(toolbar->buttons[i].image != 0)
				SDL_BlitSurface(images[toolbar->buttons[i].image], NULL,
						sf, &(SDL_Rect) {
							x + (BUTTON_W/2) - (images[toolbar->buttons[i].image]->w/2) + ((i == toolbar->selected) ? 0 : -1),
							(BUTTON_W/2) - (images[toolbar->buttons[i].image]->h/2) + ((i == toolbar->selected) ? 0 : -1)
						});
			x += BUTTON_W;
		}
		else
			x += SEPARATOR_W;
	toolbar->sf = sf;

	return toolbar;
}


void toolbar_mouse_button(Toolbar* toolbar, SDL_MouseButtonEvent b)
{
}


void free_toolbar(Toolbar* toolbar)
{
	SDL_FreeSurface(toolbar->sf);
	free(toolbar);
}
